package web

import (
	"jgt-oper/src/bll"
	"jgt-oper/src/context"
	"jgt-oper/src/schema"
	"jgt-oper/src/util"
	"strings"
)

// Role 角色管理
type Role struct {
	RoleBll *bll.Role `inject:""`
	MenuBll *bll.Menu `inject:""`
}

// QueryPage 查询分页数据
func (a *Role) QueryPage(ctx *context.Context) error {
	pageIndex, pageSize := ctx.GetPageIndex(), ctx.GetPageSize()

	params := schema.RoleQueryParam{
		Name:   ctx.Query("name"),
		Status: util.S(ctx.Query("status")).Int(),
	}

	total, items, err := a.RoleBll.QueryPage(params, pageIndex, pageSize)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}

	result := map[string]interface{}{
		"list": items,
		"pagination": map[string]interface{}{
			"total":    total,
			"current":  pageIndex,
			"pageSize": pageSize,
		},
	}

	return ctx.ResSuccess(result)
}

// QuerySelect 查询选择数据
func (a *Role) QuerySelect(ctx *context.Context) error {
	params := schema.RoleSelectParam{
		Name: ctx.Query("name"),
	}

	items, err := a.RoleBll.QuerySelect(params)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResSuccess(items)
}

// Get 精确查询数据
func (a *Role) Get(ctx *context.Context) error {
	item, err := a.RoleBll.Get(ctx.Param("id"))
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}

	// 筛选叶子节点
	if len(item.MenuIDs) > 0 {
		menus, err := a.MenuBll.QueryByIDs(item.MenuIDs)
		if err != nil {
			return ctx.ResErrInternalServer(err)
		}

		var menuIDs []string
		for _, m := range menus {
			var exists bool
			for _, m2 := range menus {
				if strings.HasPrefix(m2.LevelCode, m.LevelCode) && m2.LevelCode != m.LevelCode {
					exists = true
					break
				}
			}
			if !exists {
				menuIDs = append(menuIDs, m.RecordID)
			}
		}
		item.MenuIDs = menuIDs
	}

	return ctx.ResSuccess(item)
}

// Create 创建数据
func (a *Role) Create(ctx *context.Context) error {
	ctx.WriteOperateLog("Role.Create", "角色管理-创建数据")
	var item schema.Role
	if err := ctx.ParseJSON(&item); err != nil {
		return ctx.ResErrBadRequest(err)
	}

	item.Creator = ctx.GetUserID()
	err := a.RoleBll.Create(&item)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResOK()
}

// Update 更新数据
func (a *Role) Update(ctx *context.Context) error {
	ctx.WriteOperateLog("Role.Update", "角色管理-更新数据")
	var item schema.Role
	if err := ctx.ParseJSON(&item); err != nil {
		return ctx.ResErrBadRequest(err)
	}

	err := a.RoleBll.Update(ctx.Param("id"), &item)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResOK()
}

// DeleteMany 删除多条数据
func (a *Role) DeleteMany(ctx *context.Context) error {
	ctx.WriteOperateLog("Role.DeleteMany", "角色管理-删除多条数据")
	ids := strings.Split(ctx.Query("batch"), ",")

	for _, id := range ids {
		err := a.RoleBll.Delete(id)
		if err != nil {
			return ctx.ResErrInternalServer(err)
		}
	}

	return ctx.ResOK()
}

// Delete 删除数据
func (a *Role) Delete(ctx *context.Context) error {
	ctx.WriteOperateLog("Role.Delete", "角色管理-删除数据")
	err := a.RoleBll.Delete(ctx.Param("id"))
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResOK()
}

// Enable 启用数据
func (a *Role) Enable(ctx *context.Context) error {
	ctx.WriteOperateLog("Role.Enable", "角色管理-启用数据")
	err := a.RoleBll.UpdateStatus(ctx.Param("id"), 1)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResOK()
}

// Disable 禁用数据
func (a *Role) Disable(ctx *context.Context) error {
	ctx.WriteOperateLog("Role.Disable", "角色管理-禁用数据")
	err := a.RoleBll.UpdateStatus(ctx.Param("id"), 2)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResOK()
}
